package com.ys.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ys.common.utils.PageUtils;
import com.ys.common.utils.Query;
import com.ys.gulimall.product.dao.BrandDao;
import com.ys.gulimall.product.entity.BrandEntity;
import com.ys.gulimall.product.service.BrandService;
import com.ys.gulimall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
	
	@Resource
	private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
	    //1、获取key
	    String key = (String) params.get("key");
	    QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
	    //如果传过来的数据不是空的，就进行多参数查询
	    if (!StringUtils.isEmpty(key)) {
		    queryWrapper.eq("brand_id",key).or().like("name",key);
	    }
	
	    IPage<BrandEntity> page = this.page(
			    new Query<BrandEntity>().getPage(params),
			    queryWrapper
	    );
	
	    return new PageUtils(page);
    }
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateDetail(BrandEntity brand) {
		//保证冗余字段的数据一致
		baseMapper.updateById(brand);
		
		if (!StringUtils.isEmpty(brand.getName())) {
			//同步更新其他关联表中的数据
			categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
			
			//TODO 更新其他关联
		}
	}
	
	@Override
	public List<BrandEntity> getBrandsById(List<Long> brandIds) {
		List<BrandEntity> brandId = baseMapper.selectList(new QueryWrapper<BrandEntity>().in("brand_id", brandIds));
		return brandId;
	}
	
}